﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Delete_RoomCategory : System.Web.UI.Page
{
    private SqlDataReader sdr = null;
    DataTable dt = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Show_RCategoryList1();
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            sqlhelper.conn.Open();
            String sql1 = "select * from HM_RoomCategory where HM_RName='" + DropDownList1.SelectedValue + "'";
            SqlCommand cmd = new SqlCommand(sql1, sqlhelper.conn);
            sdr = cmd.ExecuteReader();
            dt.Load(sdr);
            if (dt.Rows.Count == 0)
            {
                Response.Write("<script>alert('没有相关数据，请重新选择！')</script>");
            }
            else
            {
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
        }
        catch
        {
            Response.Write("<script>alert('查找失败，请查看相关代码！')</script>");
        }
        finally
        {
            sdr.Close();
            sqlhelper.conn.Close();
        }
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            try
            {
                LinkButton Delete = (LinkButton)e.Row.Cells[8].Controls[0];
                Delete.OnClientClick = "return confirm('你确定要删除房间号为：" + e.Row.Cells[1].Text.Trim() + "的信息吗？');";
            }
            catch
            {
                Response.Write("<script>alert('出现异常，请查看相关代码！')</script>");
            }
        }
        Check_All.Attributes.Add("onclick", "return confirm('你确定要删除所选信息？')");
        Label1.Text = "当前页为第" + (GridView1.PageIndex + 1).ToString() + "页，共有" + (GridView1.PageCount).ToString() + "页";
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            sqlhelper.conn.Open();
            string delete = "delete from HM_RoomCategory where HM_RCategoryId='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
            SqlCommand cmd = new SqlCommand(delete, sqlhelper.conn);
            cmd.ExecuteNonQuery();
            Response.Write("<script>alert('删除成功！')</script>");
            string delete1 = "delete from HM_RoomStatus where HM_RCategoryId='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
            SqlCommand cmd1 = new SqlCommand(delete1, sqlhelper.conn);
            cmd1.ExecuteNonQuery();
            GridView1.DataBind();
        }
        catch
        {
            Response.Write("<script>alert('删除失败，请查看相关代码！')</script>");
        }
        finally
        {
            sqlhelper.conn.Close();
            Show_RCategoryList();
        }
    }

    protected void CheckAll_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox CheckAll = (CheckBox)sender;
        foreach (GridViewRow gvRow in GridView1.Rows)
        {
            CheckBox CheckItem = (CheckBox)gvRow.FindControl("CheckItem");
            CheckItem.Checked = CheckAll.Checked;
        }
    }
    protected void Check_All_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < this.GridView1.Rows.Count; i++)
        {
            if ((this.GridView1.Rows[i].Cells[0].FindControl("CheckItem") as CheckBox).Checked == true)
            {
                try
                {
                    sqlhelper.conn.Open();
                    int RoomId = (int)GridView1.DataKeys[i].Value;
                    string DeleAll = string.Format("delete from HM_RoomCategory where HM_RCategoryId={0}", RoomId);
                    SqlCommand cmd = new SqlCommand(DeleAll, sqlhelper.conn);
                    cmd.ExecuteNonQuery();
                    string delete2 = string.Format("delete from HM_RoomStatus where HM_RCategoryId={0}", RoomId);
                    SqlCommand cmd2 = new SqlCommand(delete2, sqlhelper.conn);
                    cmd2.ExecuteNonQuery();
                }
                catch
                {
                    Response.Write("<script>alert('删除失败，请查看相关代码！')</script>");
                }
                finally
                {
                    sqlhelper.conn.Close();
                }
            }
        }
        Response.Write("<script>alert('删除成功！')</script>");
        GridView1.DataBind();
        Show_RCategoryList();
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        Show_RCategoryList();
    }
    protected void Show_RCategoryList()
    {
        String sql1 = "select * from HM_RoomCategory where HM_RName='" + DropDownList1.SelectedValue + "'";
        sqlhelper.conn.Open();
        SqlDataAdapter myadapter = new SqlDataAdapter(sql1, sqlhelper.conn);
        DataSet ds = new DataSet();
        myadapter.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        ds.Clear();
        sqlhelper.conn.Close();
    }

    protected void GridViewPageSize_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridView1.PageSize = int.Parse(GridViewPageSize.SelectedValue);
        GridView1.DataBind();
        Show_RCategoryList1();
    }
    protected void Show_RCategoryList1()
    {
        String sql1 = "select * from HM_RoomCategory where HM_RName='单人间'";
        sqlhelper.conn.Open();
        SqlDataAdapter myadapter = new SqlDataAdapter(sql1, sqlhelper.conn);
        DataSet ds = new DataSet();
        myadapter.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        ds.Clear();
        sqlhelper.conn.Close();
    }
}